package com.example.simin_manage.admim.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @author: panpan
 * @time: 2025/9/14
 */
@Component
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();

        // 放行登录页、静态资源和 API
        if(uri.startsWith("/login") || uri.startsWith("/api/") || uri.endsWith(".css") || uri.endsWith(".js") || uri.endsWith(".png")) {
            return true;
        }

        // 检查 session
        Object userId = request.getSession().getAttribute("userId");
        if(userId == null) {
            response.sendRedirect("/login.html"); // 未登录重定向
            return false;
        }

        return true;
    }

}
